package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.base.UserContextInfo;
import com.mars.common.constant.Constant;
import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import com.mars.common.util.RequestUtils;
import com.mars.common.util.TokenUtils;
import com.mars.module.admin.entity.Record;
import com.mars.module.base.BaseController;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.Award;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.IAwardService;
import com.mars.module.admin.request.AwardRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 奖项控制层
 *
 * @author mars
 * @date 2024-04-27
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "奖项接口管理",tags = "奖项接口管理")
@RequestMapping("/admin/award" )
public class AwardController {

    private final IAwardService iAwardService;

    /**
     * 分页查询奖项列表
     */
    @ApiOperation(value = "分页查询奖项列表")
    @PostMapping("/pageList")
    public R<PageInfo<Award>> pageList(@RequestBody AwardRequest awardRequest) {
        return R.success(iAwardService.pageList(awardRequest));
    }

    /**
     * 获取奖项详细信息
     */
    @ApiOperation(value = "获取奖项详细信息")
    @GetMapping(value = "/query/{id}")
    public R<Award> detail(@PathVariable("id") Integer id) {
        return R.success(iAwardService.getById(id));
    }

    /**
     * 新增奖项
     */
    @Log(title = "新增奖项", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增奖项")
    @PostMapping("/add")
    public R<Void> add(@RequestBody AwardRequest awardRequest) {
        iAwardService.add(awardRequest);
        return R.success();
    }

    /**
     * 修改奖项
     */
    @Log(title = "修改奖项", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改奖项")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody AwardRequest awardRequest) {
        iAwardService.update(awardRequest);
        return R.success();
    }

    /**
     * 删除奖项
     */
    @Log(title = "删除奖项", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除奖项")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Integer[] ids) {
        iAwardService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出奖项
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody AwardRequest awardRequest) throws IOException {
        List<Award> list = iAwardService.list(awardRequest);
        ExcelUtils.exportExcel(list, Award.class, "奖项信息", response);
    }
    @PostMapping(value = "/list")
    public R<List<Award>> list(HttpServletResponse response,@RequestBody AwardRequest awardRequest) throws IOException {
        List<Award> list = iAwardService.list(awardRequest);
        return R.success(list);
    }

    TokenUtils tokenUtils;

    @PostMapping(value = "/lottery")
    public R<Award> lottery(HttpServletResponse response,@RequestBody AwardRequest awardRequest) throws IOException {
        Integer activityId = awardRequest.getActivityId();
        HttpServletRequest request = RequestUtils.getRequest();
        Long userId = tokenUtils.getUserId(request);
        return iAwardService.lottery(activityId,userId);
    }

    @PostMapping(value = "/getMyRecord")
    public R<List<Record>> getMyRecord(HttpServletResponse response, @RequestBody AwardRequest awardRequest) throws IOException {
        Integer activityId = awardRequest.getActivityId();
        HttpServletRequest request = RequestUtils.getRequest();
        Long userId = tokenUtils.getUserId(request);
        return iAwardService.getAward(activityId,userId);
    }
}
